package com.middlegroup.subjectpay.web.api;

import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.common.model.SubjectpayQueryRequest;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.bind.SessionSubjectCode;
import com.middlegroup.subjectpay.service.QueryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 *
 * @author pc
 * @date 2019/1/14
 */
@RestController
public class QueryController extends BaseController {
    @Resource
    private QueryService queryService;


    /**通用查询接口(代付，代扣，协议，绑卡等状态查询)
     curl -i 'http://10.0.0.109:8089/orderQuery' -d 'serviceType=DFQUERY&subjectCode=S000001&merchantBillNo=merchantBillNo201901270008&billNo=billNo201901230008'
     * @param queryRequest 查询参数
     * @param subjectCode 主体code
     * @return 通用json返回
     */
    @RequestMapping(value = "orderQuery")
    public ResponseEntity orderQuery(@Valid SubjectpayQueryRequest queryRequest, @SessionSubjectCode String subjectCode){
//    public ResponseEntity orderQuery(@Valid SubjectpayQueryRequest queryRequest){
        ResponseEntity ret = new ResponseEntity(ResponseEntity.STATUS_OK);
        //初始化查询结果
        TransResult transResult = new TransResult();
        transResult.setMerchantBillNo(queryRequest.getMerchantBillNo());
        transResult.setBillNo(queryRequest.getBillNo());
        transResult.setTransStatus(0);
        transResult.setMsg("查询处理中");
        transResult.setTransTime(System.currentTimeMillis());
        ret.setData(transResult);

        //调用通用查询接口
        transResult = queryService.orderQuery(queryRequest);

        if (null != transResult){
            return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
        }

        //如果通用代付查询接口异常，则默认代付处理中，防止重复交易
        transResult.setTransStatus(0);
        transResult.setMsg("查询服务异常!请联系管理员核对状态!");
        transResult.setTransTime(System.currentTimeMillis());
        return ret;
    }
}
